package com.gee.spring.cloud.data.transform.core.center.config.handler;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import com.gee.spring.cloud.common.core.api.result.ApiResult;
import com.gee.spring.cloud.common.core.entity.center.config.OrgCenter;
import com.gee.spring.cloud.common.starter.config.event.CenterConfigEventHandler;
import com.gee.spring.cloud.common.starter.feign.center.config.OrgCenterClient;
import com.gee.spring.cloud.data.transform.core.dispatch.BaseDataDispatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author gepengjun
 * @since 2024/3/19 14:31
 */
@Component
public class OrgCenterHandler extends CenterConfigEventHandler<OrgCenter> {

    // key: OrgCenter.centerIdentity
    private final Map<Long, OrgCenter> orgCenterMap = new HashMap<>();

    @Autowired
    private OrgCenterClient orgCenterClient;

    @Autowired
    private BaseDataDispatcher baseDataDispatcher;

    public OrgCenter getByCenterIdentity(Long centerIdentity){
        return orgCenterMap.computeIfAbsent(centerIdentity, ci -> {
            ApiResult<OrgCenter> orgCenterApiResult = orgCenterClient.getByCenterIdentity(centerIdentity);
            if (!orgCenterApiResult.isSuccess() || orgCenterApiResult.getData() == null) {
                throw new RuntimeException("未找到中心, 地址编码: " + StrUtil.fillBefore(HexUtil.toHex(centerIdentity), '0', 8));
            }
            return orgCenterApiResult.getData();
        });
    }

    public OrgCenter getByOrgCenterId(Long centerId){
        Optional<OrgCenter> orgCenterOptional = orgCenterMap.values().stream()
                .filter(orgCenter -> orgCenter.getId().equals(centerId))
                .findFirst();
        if (orgCenterOptional.isPresent()){
            return orgCenterOptional.get();
        }else {
            ApiResult<OrgCenter> orgCenterApiResult = orgCenterClient.getById(centerId);
            OrgCenter orgCenter = orgCenterApiResult.getData();
            if (orgCenterApiResult.isSuccess() && orgCenter != null){
                return orgCenter;
            }else {
                throw new RuntimeException("根据中心id未找到中心: " + centerId);
            }
        }
    }

    @Override
    public void handlerUpdate(List<OrgCenter> dataList, List<OrgCenter> oldList) {
        for (int i = 0; i < dataList.size(); i++) {
            OrgCenter orgCenter = dataList.get(i);
            OrgCenter oldOrgCenter = oldList.get(i);
            orgCenterMap.computeIfPresent(oldOrgCenter.getCenterIdentity(), (newKey, old) -> orgCenter);
            baseDataDispatcher.updateOrgCenter(orgCenter, oldOrgCenter);

        }
    }

    @Override
    public void handlerDelete(List<OrgCenter> oldList) {
        oldList.forEach(orgCenter -> {
            orgCenterMap.remove(orgCenter.getCenterIdentity());
            baseDataDispatcher.removeCenter(orgCenter);
        });
    }
}
